/**
 * Created by yaohuan on 2018/3/26 0026.
 */
define(function () {

    var self, pageUi,content;
    var item = app.pageParameters['pjqsinfo_page'].item;//获取传的参数
    var user = app.userInfos.sysUser;

    function Index() {
        self = this;
        self.data = {
            MMRV001:item.MMRUDOCNO,
            MMRV002:item.MMRTSEQ,
            MMRV003:item.MMRT003,
            MMRV004:item.MMRU004,
            MMRV005:'',//签收数量
            MMRV006:item.MMRU006,
            MMRV007:user.MOBILE,
            MMRV008: item.MMRT011,//签收地址
            MMRV009:'',//照片地址
            MMRV010:item.MMRU015,//服务单号
            MMRV011:item.MMRU016,//客户
            MMRV012:item.MMRU017,//备注
            MMRS001:item.MMRS001,
            MMRS002:item.MMRS002,
            MMRS003:item.MMRS003,
            MMRS004:item.MMRS004,
            MMRS005:item.MMRS005,
            MMRS006:item.MMRS006,
            MMRS008:item.MMRS008,

            MMRVDOCDATE: ''
        };
        self.imgArr = [];
        self.init();
    }

    var indexPro = Index.prototype;

    indexPro.getLocation = function () {
        app.regeoByLocation({
            cb: function (res) {
                self.data.MMRV008 = res.formatted_address;
            },
            // lat: 30.2361,
            // lng: 120.18663
        });

        self.data.MMRVDOCDATE = (new Date()).Format('yyyy-MM-dd hh:mm:ss');
    };

    indexPro.sure = function () {

       app.showLoading();
       app.uploadLocalIds({
            ids: self.imgArr,
            cb: function (imgs) {
                var imgurl = "";
                for (var i = 0; i < imgs.length; i++) {
                    imgurl += (imgs[i].name + ",");
                }
                content.d.MMRV009=imgurl.substring(0,imgurl.length-1);
                //content.d.MMRV009 = imgs.join(',');

                var _data = {
                    CHARACTERISTICS: []
                }
                for (var i = 0; i < item.length; i++) {
                    _data.CHARACTERISTICS.push({
                        MMRV001:item[i].MMRUDOCNO,
                        MMRV002:item[i].MMRUSEQ,
                        MMRV003:item[i].MMRT003,
                        MMRV004:item[i].MMRU004,
                        MMRV005:item[i].MMRV005,//签收数量
                        MMRV006:item[i].MMRU006,
                        MMRV007:user.MOBILE,
                        MMRV008: content.d.MMRV008,//签收地址
                        MMRV009:content.d.MMRV009,//照片地址
                        MMRV010:item[i].MMRU015,//服务单号
                        MMRV011:item[i].MMRU016,//客户
                        MMRV012:item[i].MMRU017,//备注
                        MMRS001:item[i].MMRS001,
                        MMRS002:item[i].MMRS002,
                        MMRS003:item[i].MMRS003,
                        MMRS004:item[i].MMRS004,
                        MMRS005:item[i].MMRS005,
                        MMRS006:item[i].MMRS006,
                        MMRS008:item[i].MMRS008,
                        MMRVDOCDATE: content.d.MMRVDOCDATE
                    })
                }

                app.ajax({
                    xurl: "api/wx/pjqs/pjqsSave",
                    loading: true,
                    xdata: {
                        obj: app.jsonString(_data)
                    },
                    cb: function (result) {
                        app.hideLoading();
                        if (result.ok) {
                            app.toast('配件签收成功！');
                            setTimeout(function () {
                                mui.back();
                                app.pageModule('p_pjqs').refesh();
                            }, 600);
                        } else {
                            app.toast(result.data.message);
                        }
                    }
                });
            }
        });
    };

    indexPro.init = function () {

        mui.ready(function () {

            content = new Vue({
                el: doc.querySelector('.mui-scroll'),
                data: {
                    d:self.data,
                    imgs: self.imgArr
                },
                methods: {
                    delimg: function (i) {
                        self.imgArr.removei(i);
                    },
                    addImgs: function () {
                        app.chooseImageNew(function (imgs) {
                            //self.imgArr.pushArray(imgs);
                            //app.extend(self.imgArr, imgs);
                            for (var i = 0; i < imgs.length; i++) {
                                self.imgArr.push({
                                    img: imgs[i].img,
                                    id: imgs[i].localId
                                })
                            }
                        });
                    },
                    showMap: function () {
                        app.openPage({
                            src: "../../html/wx/map.html",
                            argsKey: 'map_page', // 设置页面请求参数key值
                            args: {
                                cb: function (result) {
                                    // 更新数据
                                    self.data.MMRV008 = result.address;
                                }
                            }
                        });
                    }
                }
            });



            pageUi = {
                signatureDiv: $('.mui-backdrop'),
            };

            mui('.mui-scroll-wrapper').scroll({
                deceleration: 0.0005
            });

            app.taps.call(win, [
                {
                    o: 'btn_save',
                    cb: function (e) {
                       indexPro.sure();
                    },
                    confirm: {
                        msg: '确认现在签收？'
                    }
                }
            ]);

            self.getLocation();

        });
    };

    return new Index();
})